var fs = require( 'fs' );
var ws=require('ws');
var https=require('https');
var app=require("express")();
var mysql = require('../conf/mysql.js');
var certOpt={
    key: fs.readFileSync('../public/privatekey.pem'),
    cert: fs.readFileSync('../public/certificate.pem'),
}
var server=https.createServer(certOpt,app).listen(8989);
var webSocket=ws.Server;
var wss=new webSocket({
    server:server
});
var count=0;
wss.on("connection",function(wsConnect){
    wsConnect.on( 'message', function ( message ) {
        var conn=mysql.connect();
        var data=JSON.parse(message);
        var type=data.type;
        if(type=="userlogin"){
            var user=[];
            user["userid"]=data.userid;
            user["username"]=data.username;
            user["updatetime"]=Date.now();
            //添加缓存数据
            mysql.insertChaCe(conn,user,function(err,resluts){
                if(err){
                   return console.log("添加现在用户失败")
                }else{
                    mysql.countuser(conn,function(err,resluts){
                        count=resluts[0]["count"];
                        wsConnect.send('{"type":"login","username":"'+data.username+'","userimg":"'+data.userimg+'","count":"'+count+'"}');
                        return console.log("添加现在用户成功")
                    })
                }
            })

        }
        if(type=="say"){
            wsConnect.send('{"type":"say","userimg":"'+data.userimg+'","msg":"'+data.msg+'"}');
        }
        if(type=="online"){
            var user=[];
            user["userid"]=data.userid;
            user["updatetime"]=Date.now();
            //添加缓存数据
            mysql.updateChaCe(conn,user,function(err,resluts){
                if(err){
                    return console.log("修改用户在线时间失败")
                }else{
                    return console.log("修改用户在线时间成功")
                }
            })
        }
    });
        //心跳包
    setInterval(function(){
            var conn=mysql.connect();
            mysql.getChaCe(conn,function(err,resluts){
                if(resluts.length>0){
                    for(var i in resluts){
                        var userid=resluts[i]["userid"];
                        var username=resluts[i]["username"];
                        var updatetime=resluts[i]["updatetime"];
                        if((Date.now()-updatetime)>5000){
                            mysql.deleteChaCe(conn,userid,function(err,resluts){
                                if(err){
                                    console.log("删除下线用户失败")
                                }else{
                                    count--;
                                    console.log("删除下线用户成功")
                                    mysql.getChaCe(conn,function(err,resluts){
                                        if(resluts.length>0){
                                            wsConnect.send('{"type":"close","username":"'+username+'","count":"'+count+'"}');
                                        }
                                    })
                                }
                            })
                        }
                    }
                }
            })
        },2000)


})

